package com.ke.jun.bg.petpackmvvm.base

import android.app.Application
import android.content.ContentProvider
import android.content.ContentValues
import android.content.IntentFilter
import android.database.Cursor
import android.net.ConnectivityManager
import android.net.Uri
import androidx.lifecycle.ProcessLifecycleOwner
import com.ke.jun.bg.petpackmvvm.ext.lifecycle.KtxActivityManger
import com.ke.jun.bg.petpackmvvm.ext.lifecycle.KtxAppLifeObserver
import com.ke.jun.bg.petpackmvvm.ext.lifecycle.KtxLifeCycleCallBack
import com.ke.jun.bg.petpackmvvm.network.manager.NetworkStateReceive

val appContext :Application by lazy { Ktx.app }
class Ktx:ContentProvider() {

    companion object {
        lateinit var app:Application
        private var mNetworkReceiver:NetworkStateReceive?=null
        var watchActivityLife=true
        var watchAppLife=true
    }
    override fun onCreate(): Boolean {
        val application=context?.applicationContext as  Application
        install(application)
         return true

    }

    private fun install(application: Application) {
        app= application
        mNetworkReceiver= NetworkStateReceive()
        app.registerReceiver(mNetworkReceiver,
            IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))

        if (watchActivityLife)application.registerActivityLifecycleCallbacks(KtxLifeCycleCallBack())
        if (watchAppLife) ProcessLifecycleOwner.get().lifecycle.addObserver(KtxAppLifeObserver)
    }

    override fun query(
        p0: Uri,
        p1: Array<out String>?,
        p2: String?,
        p3: Array<out String>?,
        p4: String?,
    ): Cursor? =null

    override fun getType(p0: Uri): String? =null

    override fun insert(p0: Uri, p1: ContentValues?): Uri? =null

    override fun delete(p0: Uri, p1: String?, p2: Array<out String>?)=0

    override fun update(p0: Uri, p1: ContentValues?, p2: String?, p3: Array<out String>?): Int =0

}